package com.itheima.controller;

import com.aliyuncs.utils.StringUtils;
import com.itheima.VO.loginVo;
import com.itheima.bean.User;
import com.itheima.common.R;
import com.itheima.service.UserService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Transactional
@RestController
@Slf4j
@RequestMapping("/user")
public class  UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 发送4位验证码
     *
     * @param phone
     * @param session
     * @return
     */
    @GetMapping("/sendMsg")
    public R sendMsg(String phone, HttpSession session) {
        System.out.println(phone);
        //生成随机4位验证码
        if (!StringUtils.isEmpty(phone)) {
            String code = ValidateCodeUtils.generateValidateCode4String(4);
            System.out.println(code);
            //调用阿里云提供的短信服务API完成发送短信

//            SMSUtils.sendMessage("瑞吉外卖","",phone,code);

            //将生成的验证码保存到Redis
            redisTemplate.opsForValue().set(phone,code, 5,TimeUnit.MINUTES);


          /*  //需要将生成的验证码保存到Session
            session.setAttribute(phone, code);*/
            return R.success(code);
        }

        return R.error("验证码发送失败！！！");
    }

    /**
     * 登录验证
     *
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Map map, HttpSession session) {
        log.info(map.toString());

        String phone = (String) map.get("phone");
        String code = (String) map.get("code");

//        String code1 = (String) session.getAttribute(phone);

        String code1 =(String) redisTemplate.opsForValue().get(phone);
        System.out.println("code1:***************************"+code1);
        User user = userService.selectByPhone(phone);
        if (user != null && code.equalsIgnoreCase(code1)) {
            //如果用户存在，且验证码正确，说明登陆成功，并将用户存储到session服务器
            session.setAttribute("user", user);

            return R.success("登录成功！！！");
        } else if (user == null && code.equalsIgnoreCase(code1)) {
            //验证码正确，但用户不存在，创建新用户，且登陆成功,并将用户存储到session服务器
            User user1 = new User();
            user1.setPhone(phone);
            user1.setStatus(1);
            userService.insert(user1);
            session.setAttribute("user", user1);
            return R.success("登陆成功！！！");
        } else return R.error("登录失败！！！");
    }

    @PostMapping("/loginout")
    public R loginout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return R.success("退出登录成功");
    }


}
